package org.extremengine.demo;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTabbedPane;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.extremengine.TimerThread;
import org.extremengine.World;
import org.extremengine.demo.BasePanelWorld.DemoCallback;

/**
 *
 */
public class ExtremEngineDemo extends javax.swing.JFrame {

	private JMenuItem miHelpAbout;
	private JMenu jMenu5;
	private JToolBar toolbar;
	private JPanel tab5;
	private JPanel tab6;
	private JButton btnStop;
	private JPanel tab0;
	private JButton btnStartOrPause;
	private JPanel tab4;
	private JPanel tab3;
	private JPanel tab2;
	private JPanel tab1;
	private JTabbedPane tabPnlContent;
	private JPanel pnlContent;
	private JMenuItem exitMenuItem;
	private JSeparator jSeparator2;
	private JMenuItem closeFileMenuItem;
	private JMenuItem saveAsMenuItem;
	private JMenuItem saveMenuItem;
	private JMenuItem openFileMenuItem;
	private JMenuItem newFileMenuItem;
	private JMenu jMenu3;
	private JMenuBar mainMenu;
	
	private BasePanelWorld currentPanel;
	private JPanel pnlFooter;
	private JLabel lblMessage;
	private JPanel tabMario;
	private SettingPanel pnlSetting;
	private JLabel label;
	private JButton btnRestart;

	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				ExtremEngineDemo inst = new ExtremEngineDemo();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	
	public ExtremEngineDemo() {
		super();
		initGUI();
		
//		this.tabPnlContentStateChanged(null);
	}
	
	private void initGUI() {
		
		// 回调处理
		DemoCallback demoCallback = new DemoCallback() {
			@Override
			public void onInited(World world) {
				pnlSetting.init(world);
			}

			@Override
			public long onTita(long time, long fps) {
				long ts = time/1000;
				long hour = ts/3600;
				long minutes = (ts - hour * 3600) /60;
				long second = ts - hour * 3600 - minutes * 60;
				String title = "FPS: " + fps + ", Time:" + hour + ":" + minutes + ":" + second;
				setTitle(title);
				return 0;
			}

			@Override
			public void onWorldStatusChanged(int status) {
				btnStartOrPauseActionPerformed(null);
			}
		};
		
		try {
			{
				pnlContent = new JPanel();
				BorderLayout pnlContentLayout = new BorderLayout();
				pnlContent.setLayout(pnlContentLayout);
				getContentPane().add(pnlContent, BorderLayout.CENTER);
				{
					tabPnlContent = new JTabbedPane();
					pnlContent.add(tabPnlContent, BorderLayout.CENTER);
					tabPnlContent.addChangeListener(new ChangeListener() {
						public void stateChanged(ChangeEvent evt) {
							tabPnlContentStateChanged(evt);
						}
					});
					{
						tab0 = new PanelSimple(demoCallback);
						tabPnlContent.addTab("静止物体", null, tab0, null);
					}
					{
						tab1 = new PanelFalling(demoCallback);
						tabPnlContent.addTab("自由落体", null, tab1, null);
						tab1.setPreferredSize(new java.awt.Dimension(645, 372));
					}
					{
						tab2 = new PanelRolling(demoCallback);
						tabPnlContent.addTab("水平碰撞", null, tab2, null);
					}
					{
						tab3 = new PanelThrow(demoCallback);
						tabPnlContent.addTab("抛物线运动", null, tab3, null);
					}
					{
						tab4 = new PanelThrow2(demoCallback);
						tabPnlContent.addTab("平抛运动", null, tab4, null);
					}
					{
						tab5 = new PanelAirCollide(demoCallback);
						tabPnlContent.addTab("空中碰撞", null, tab5, null);
					}
//					{
//						tab6 = new PanelPingPong(demoCallback);
//						tabPnlContent.addTab("弹球", null, tab6, null);
//					}
					
					{
						tabMario = new PanelMario(demoCallback);
						tabPnlContent.addTab("超级玛丽", null, tabMario, null);
					}
					tabPnlContent.setFocusable(false);
				}
			}
			{
				toolbar = new JToolBar();
				getContentPane().add(toolbar, BorderLayout.NORTH);
				{
					btnStartOrPause = new JButton();
					toolbar.add(btnStartOrPause);
					btnStartOrPause.setText("\u5f00\u59cb");
					btnStartOrPause.setFocusable(false);
					btnStartOrPause.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							btnStartOrPauseActionPerformed(evt);
						}
					});
				}
				{
					btnStop = new JButton();
					btnStop.setEnabled(false);
					toolbar.add(btnStop);
					btnStop.setText("\u505c\u6b62");
					btnStop.setFocusable(false);
					{
						btnRestart = new JButton("重启");
						btnRestart.addActionListener(new ActionListener() {
							public void actionPerformed(final ActionEvent e) {
								handleBtnRestartActionPerformed(e);
							}
						});
						toolbar.add(btnRestart);
					}
					{
						pnlFooter = new JPanel();
						getContentPane().add(pnlFooter, BorderLayout.SOUTH);
						pnlFooter.setLayout(new BorderLayout(0, 0));
						{
							lblMessage = new JLabel(":");
							pnlFooter.add(lblMessage);
						}
					}
					{
						pnlSetting = new SettingPanel();
						getContentPane().add(pnlSetting, BorderLayout.WEST);
						pnlSetting.init(currentPanel.world);
					}
					btnStop.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							btnStopActionPerformed(evt);
						}
					});
				}
			}
			this.setSize(917, 507);
			{
				mainMenu = new JMenuBar();
				setJMenuBar(mainMenu);
				{
					jMenu3 = new JMenu();
					mainMenu.add(jMenu3);
					jMenu3.setText("File");
					{
						newFileMenuItem = new JMenuItem();
						jMenu3.add(newFileMenuItem);
						newFileMenuItem.setText("New");
					}
					{
						openFileMenuItem = new JMenuItem();
						jMenu3.add(openFileMenuItem);
						openFileMenuItem.setText("Open");
					}
					{
						saveMenuItem = new JMenuItem();
						jMenu3.add(saveMenuItem);
						saveMenuItem.setText("Save");
					}
					{
						saveAsMenuItem = new JMenuItem();
						jMenu3.add(saveAsMenuItem);
						saveAsMenuItem.setText("Save As ...");
					}
					{
						closeFileMenuItem = new JMenuItem();
						jMenu3.add(closeFileMenuItem);
						closeFileMenuItem.setText("Close");
					}
					{
						jSeparator2 = new JSeparator();
						jMenu3.add(jSeparator2);
					}
					{
						exitMenuItem = new JMenuItem();
						jMenu3.add(exitMenuItem);
						exitMenuItem.setText("Exit");
					}
				}
				{
					jMenu5 = new JMenu();
					mainMenu.add(jMenu5);
					jMenu5.setText("Help");
					{
						miHelpAbout = new JMenuItem();
						jMenu5.add(miHelpAbout);
						miHelpAbout.setText("About");
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


	private void btnStartOrPauseActionPerformed(ActionEvent evt) {
		System.out.println("btnStartOrPause.actionPerformed, event="+evt);
		Component c = tabPnlContent.getSelectedComponent();
		if(c instanceof BasePanelWorld) {
			BasePanelWorld pw = (BasePanelWorld)c;
			if(pw.world.getStatus() == TimerThread.STATUS_STOPED) {
				pw.startDemoWorld();
				btnStartOrPause.setText("暂停");
			}
			else if(pw.world.getStatus() == TimerThread.STATUS_RUNNING ) {
				pw.pauseDemoWorld();
				btnStartOrPause.setText("继续");
			}
			else if(pw.world.getStatus() == TimerThread.STATUS_PAUSED) {
				pw.resumeDemoWorld();
				btnStartOrPause.setText("暂停");
			}
			btnStop.setEnabled(true);
		}
	}
	
	private void btnStopActionPerformed(ActionEvent evt) {
		System.out.println("btnStop.actionPerformed, event="+evt);
		Component c = tabPnlContent.getSelectedComponent();
		if(c instanceof BasePanelWorld) {
			((BasePanelWorld)c).stopDemoWorld();
			btnStop.setEnabled(false);
			btnStartOrPause.setText("开始");
		}
	}

	protected void handleBtnRestartActionPerformed(final ActionEvent e) {
		System.out.println("btnRestart.actionPerformed, event=" + e);
		Component c = tabPnlContent.getSelectedComponent();
		if (c instanceof BasePanelWorld) {
			BasePanelWorld pw = (BasePanelWorld) c;
			if (pw.world.getStatus() == TimerThread.STATUS_RUNNING) {
				pw.stopDemoWorld();
			}
			pw.startDemoWorld();
			btnStop.setEnabled(true);
			btnStartOrPause.setText("暂停");
		}
	}
	
	private void tabPnlContentStateChanged(ChangeEvent evt) {
		System.out.println("tabPnlContent.stateChanged, event=" + evt);
		if (currentPanel != null && currentPanel.world.isRunning()) {
			currentPanel.world.pause();
		}
		Component c = tabPnlContent.getSelectedComponent();
		currentPanel = (BasePanelWorld) c;
//		currentPanel.initDemoWorld();// Init demo World before using it.
		currentPanel.setFocusable(true);
		currentPanel.grabFocus();
		if (pnlSetting != null) {
			pnlSetting.init(currentPanel.world);
		}
//		System.out.println("Current: " + c);
	}

}
